package com.wit.witmg.service.impl;

import com.alibaba.fastjson.JSON;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.wit.witmg.entity.TLogist;
import com.wit.witmg.entity.TLogists;
import com.wit.witmg.mapper.TLogistsMapper;
import com.wit.witmg.service.ITLogistsService;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.wit.witmg.util.ErrorCode;
import com.wit.witmg.util.JsonResult;
import org.springframework.stereotype.Service;

import javax.servlet.http.HttpServletRequest;

/**
 * <p>
 *  服务实现类
 * </p>
 *
 * @author bob
 * @since 2025-02-24
 */
@Service
public class TLogistsServiceImpl extends ServiceImpl<TLogistsMapper, TLogists> implements ITLogistsService {

    @Override
    public Object add(HttpServletRequest request, String json) {
        TLogists t= JSON.parseObject(json,TLogists.class);
        if(t.getId()>0){
            boolean bl=  updateById(t);
            if(!bl){
                return JsonResult.error(ErrorCode.ERR_200);
            }
        }else{
            boolean bl= save(t);
            if(!bl){
                return JsonResult.error(ErrorCode.ERR_504);
            }
        }
        return JsonResult.success(t);
    }

    @Override
    public Object get(HttpServletRequest request, Integer pid, Long phone) {
        QueryWrapper<TLogists> qw=new QueryWrapper<>();
        if(pid!=null){
            qw.eq("pid",pid);
            return JsonResult.success(list(qw));
        }else if(phone!=null){
            qw.eq("phone",phone);
            qw.last("limit 1");
            return JsonResult.success(getOne(qw));
        }

        return JsonResult.error(ErrorCode.ERR_200);
    }
}
